
export default class TtUtil {
    static mUserInfoButton = null;
    /**
     * 这是微信小游戏里面的通用弹窗
     * @param showContent 显示的标题
     * @param iconFlag 图标（success/loading）
     * @param mDuration 延迟的时间
     */
    public static showWxText(showContent, iconFlag = 1, mDuration = 1500) {
        var iconType = iconFlag == 0 ? 'success' : 'loading';
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.showToast({
                title: showContent,
                icon: iconType,
                duration: mDuration
            })
        }
    }

    public static showLoading(title) {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            tt.showLoading({
                title: "" + title,
            });
        }
    }
    public static hideLoading() {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            tt.hideLoading({

            });
        }
    }
    /**
     * 查询是否授权
     */
    static selectEmpower() {
        return new Promise((resolve, reject) => {
            if (cc.sys.platform != cc.sys.WECHAT_GAME) {
                resolve(true);
                return;
            }
            Log('查询授权')
            tt.getSetting({
                success(res) {
                    // 用户同意授权用户信息
                    Log('查询结果', res)
                    if (res.authSetting['scope.userInfo'] == false) {
                        resolve(false)
                    }
                    else {
                        resolve(true)
                    }
                },
                fail(res) {
                    Log('查询失败,',res)
                    resolve(false)
                }
            });
        })
    }
    //请求用户是否授权 禁用
    static queryEmpower() {
        return new Promise((resolve, reject) => {
            if (cc.sys.platform != cc.sys.WECHAT_GAME) {
                resolve(true);
                return;
            }
            Log('开始授权了哦')
            tt.authorize({
                scope: "scope.userInfo",
                success() {
                    // 用户同意授权用户信息
                    Log('用户竟然授权了')
                    resolve(true)
                },
                fail() {
                    Log('授权失败')
                    resolve(false)
                }
            });
        })

    }
    public static ttLogin() {
        Log('开始登录1')
        return new Promise((resolve, reject) => {
            if (cc.sys.platform == cc.sys.WECHAT_GAME && tt && tt.login) {
                Log('开始登录2')
                tt.login({
                    force : false,
                    success(res) {
                        Log(`login调用成功${res.code} ${res.anonymousCode}`);
                        resolve(1);
                    },
                    fail(res) {
                        Log(`login调用失败`);
                        TtUtil.showWxText('用户登录失败!')
                        reject(2);
                    }
                });
            }
            else {
                resolve(1);
            }
        })

    }
    /**
     * 邀请分享
     * @param query 
     */
    static shareAppMessage(query = '', back?) {
        tt.shareAppMessage({
            templateId: "1ek3ab7akm0d9ir3kj", // 替换成通过审核的分享ID
            query: query,
            success() {
                console.log("分享成功");
                back && back();
            },
            fail(e) {
                console.log("分享失败");
            }
        });
    }
    /**
     * 获取玩家信息
     * @param succ 成功回调
     * @param fail 失败回调
     */
    public static getUserInfo(succ, fail) {
        tt.getUserInfo({
            success: function (res) {
                Log('获取权限成功!:', res)
                succ && succ(res);
            },
            fail: function (res) {
                Log('获取权限失败!:', res)
                fail && fail(res);
                // iOS 和 Android 对于拒绝授权的回调 errMsg 没有统一，需要做一下兼容处理
                // if (res.errMsg.indexOf('auth deny') > -1 || res.errMsg.indexOf('auth denied') > -1) {
                //     // 处理用户拒绝授权的情况
                //     self.noAuthorizeOpenSetting(function (result) {
                //         back('success', result);
                //     });
                //     back('fail', '用户拒绝授权');
                // }
            }
        })
    }
    /**
     * 获取微信登录信息
     */
    public static getLaunchShareInfo() {
        if (cc.sys.platform != cc.sys.WECHAT_GAME) {
            Log('不是抖音哦,别瞎几把搞')
            return {};
        }
        let launchOption = wx.getLaunchOptionsSync();
        Log('登录所带信息:', JSON.stringify(launchOption));
        let info = launchOption.query;
        return info
    }
    static createUserInfoButton(callback) {
        let self = this;
        // UIHelper.hideLoading();
        wx.getSystemInfo({
            success: function (res) {
                self.mUserInfoButton = wx.createUserInfoButton({
                    type: 'text',
                    text: '0',
                    style: {
                        left: 0,
                        top: 0,
                        width: res.screenWidth,
                        height: res.screenHeight,
                        lineHeight: 0,
                        backgroundColor: '#00000000',
                        color: '#00000000',
                        textAlign: 'center',
                        fontSize: 16,
                        borderRadius: 4
                    }
                })
                self.mUserInfoButton.onTap((res) => {
                    if (res.rawData) {
                        if (callback) {
                            callback(true);
                        }
                    }
                    else {
                        if (callback) {
                            callback(false);
                        }
                    }
                })
            }
        });
    }
    static hideUserInfoButton() {
        let self = this;
        if (self.mUserInfoButton) {
            self.mUserInfoButton.destroy();
        }
    }
    /** 手动获取用户信息 */
    static getAuthorize(succ,fail?) {
        tt.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo'] == false) {
                    tt.showModal({
                        title: "提示",
                        content: "您的用户信息未授权，请您确定重新获取授权哦～",
                        success: (res) => {
                            if (res.confirm) {
                                tt.openSetting({
                                    success: res => {
                                        if (res.authSetting['scope.userInfo']) {
                                            this.loadUserInfo(succ,fail)
                                        }
                                    }
                                })
                            }
                        }
                    })
                } else {
                    this.loadUserInfo(succ,fail)
                }
            }
        })
    }
    /** 获取用户授权状态与信息 */
    private static loadUserInfo(succ,fail) {
        tt.login({
            force: false,
            success: res => {
                tt.getUserInfo({
                    success: res => {
                        Log("用户信息:", res);
                        succ && succ(res);
                    },
                    fail: err => {
                        Log("用户信息 err:", err);
                        TtUtil.showWxText('获取信息失败')
                        fail && fail();
                    }
                })
            },
            fail : (res)=>{
                TtUtil.showWxText('用户登录失败')
            }
        })
    }
}
